import { byte2src } from "@/utils";
import { doPostDowload } from "@/utils/request";
import axios from "axios";
import { onMounted, ref } from "vue";
import { useRoute } from "vue-router";
import { message } from "ant-design-vue";

export const useGetImagesFromPdf = ({ spinning, tip }) => {
  const route = useRoute();
  const fileId = route.params.id;
  const token = route.query.token;

  //文档展示的数据  包含控件列表
  const documentPDF = ref({
    images: [],
    control: [],
  });

  const getImagesFromPdf = async () => {
    tip.value = "加载中...";
    spinning.value = true;
    // 调用接口获取图片
    axios({
      baseURL: "/ucop/customer/webapi/",
      method: "get",
      url: "/pdfConvertImage",
      params: { fileId },
      headers: {
        Authorization: token || "-",
      },
    })
      .then((res) => {
        console.log("res: ", res);
        if (!res.data.success) {
          message.error(res.data.message);
          return;
        }

        documentPDF.value.images = res.data.result.map((i) => ({
          image: "data:image/png;base64," + i,
        }));
      })
      .finally(() => {
        spinning.value = false;
      });
  };

  onMounted(() => {
    getImagesFromPdf();
  });

  return {
    documentPDF,
  };
};
